<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Activity_model extends CI_Model
{
	private $activity_info_table_name	= 'activity_info';
	private $activity_pool_table_name	= 'activity_pool';
	
	function __construct()
	{
		parent::__construct();
		$ci =& get_instance();
	}
	
	function delete_by_kind($activity_id,$kind){
		$this->db->delete($this->activity_pool_table_name, array('activity_id' => $activity_id,'kind'=>$kind));
		if($this->db->affected_rows() > 0 ) return true;
		else return false; 
	}

	function save($data){
		$remove_keys = array('file'=>'','save'=>'');
		$data['upload_image'] = isset($data['upload_image'])?json_encode($data['upload_image']):'';
		$data = array_diff_key($data, $remove_keys);
		
		//echo "<pre>in save:";print_r($data);echo "</pre>";exit;
		
		$this->db->insert($this->activity_info_table_name, $data);
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function modify($data,$activity_id){
		$remove_keys = array('file'=>'','save'=>'');
		$data['upload_image'] = isset($data['upload_image'])?json_encode($data['upload_image']):'';
		$data['status'] = '1';//set draft to published
		$data = array_diff_key($data, $remove_keys);
		
		//echo "<pre>in modify:";print_r($data);echo "</pre>";exit;
		$this->db->where('activity_id', $activity_id);
		$this->db->update($this->activity_info_table_name, $data);
		
		if($this->db->affected_rows() > 0 ) return true;
		else return false;
	}
	
	function get($user_id){
		$activities = array();
		// get organizer establish activities first
		$this->db->order_by("activity_id", "desc");
		$query = $this->db->get_where($this->activity_info_table_name, array('user_id' => $user_id));
		
		$activities['organizer'] = $query->result_array();
		
		$this->db->select('*');
		$this->db->from($this->activity_pool_table_name);
		$this->db->join($this->activity_info_table_name, $this->activity_pool_table_name.'.activity_id = '.$this->activity_info_table_name.'.activity_id');
		$this->db->where($this->activity_pool_table_name.'.user_id',$user_id);
		$this->db->order_by($this->activity_info_table_name.".activity_id", "desc"); 
		$query = $this->db->get();
		
		foreach($query->result_array() as $key=>$val){
			$activities[$val['kind']][] = $val;
		}
		
		return $activities;
	}
	
	function get_by_kind($kind,$user_id){
		$this->db->select('*');
		$this->db->from($this->activity_pool_table_name);
		$this->db->join($this->activity_info_table_name, $this->activity_pool_table_name.'.activity_id = '.$this->activity_info_table_name.'.activity_id');
		$this->db->where($this->activity_pool_table_name.'.user_id',$user_id);
		$this->db->where($this->activity_pool_table_name.'.kind',$kind);
		$this->db->order_by($this->activity_info_table_name.".activity_id", "desc"); 
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_published_activity(){
		$this->db->select('*');
		$this->db->from($this->activity_info_table_name);
		$this->db->where('status','1');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_published_activity_by_condition($data){
		$current_date = date('Y-m-d');
		$this->db->select('*');
		$conditions = array('activity_location' => $data['activity_location'], 'activity_category' => $data['activity_category']);
		$this->db->like($conditions); 
		$this->db->from($this->activity_info_table_name);
		$this->db->where('activity_date >=', $current_date);
		$this->db->where('activity_date <=', $data['activity_date']);
		$this->db->where('status','1');
		$query = $this->db->get();
		//echo $this->db->_error_message();exit;
		//echo "<pre>";print_r($this->db->_error_message());echo "</pre>";exit;
		if($this->db->_error_message()){
			return false;
		}
		else{
			return $query->result_array();
		}
		
	}
	
	function get_all_published_activity_by_create_date(){
		$this->db->select('*');
		$this->db->from($this->activity_info_table_name);
		$this->db->order_by("modified_time", "desc");
		$this->db->limit(5);
		$this->db->where('status','1');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_join_count($activity_id){
		
		$this->db->from($this->activity_pool_table_name);
		$this->db->where($this->activity_pool_table_name.'.activity_id',$activity_id);
		$this->db->where($this->activity_pool_table_name.'.kind','join');
		 
		return $this->db->count_all_results();
	}

	function get_by_id($activity_id,$user_id){
		$activities = array();
		// get organizer establish activities first
		$query = $this->db->get_where($this->activity_info_table_name, array('user_id' => $user_id,'activity_id'=>$activity_id));
		
		return $query->result_array();
	}
}
